package com.benyi.util;


import java.lang.reflect.Field;
import java.text.SimpleDateFormat; 
import java.util.HashMap;
import java.util.Locale;
import java.util.Map; 

public class MyJson {
    private static SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
    
    
    public static String toString(Object obj) {
        StringBuilder json = new StringBuilder();
        json.append("{");
        Field[] fields = obj.getClass().getDeclaredFields();
        for (int i = 0; i < fields.length; i++) { 
            try {
            	fields[i].setAccessible(true);
            	String name = fields[i].getName() ;
            	Object v = fields[i].get(obj);
            	if(v!=null) {
            		if(json.length()>1) {
            			json.append(";");
            		}
            		json.append(name).append("=");
                    json.append(v.toString()); 
            	}  
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        json.append("}");
        return json.toString();
    }
 
    public static <T> T toObject(String jsonString, Class<T> clazz) {
        try {
            T obj = clazz.newInstance();
            jsonString = jsonString.substring(1, jsonString.length() - 1); // 去掉大括号
            if(jsonString.length()==0) {
            	return null;
            }
            String[] keyValuePairs = jsonString.split(";");
            Map<String, String> map = new HashMap<>();
            for (String pair : keyValuePairs) {
                String[] entry = pair.split("=");
                map.put(entry[0], entry[1]);
            }
            for (Field field : clazz.getDeclaredFields()) {
            	String name = field.getName() ;
            	String value = map.get(name);
            	if(value != null) {
            		field.setAccessible(true);
                    field.set(obj, convertValue(field.getType(),value));
            	} 
            }
            return obj;
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 根据字段类型转换值
    private static Object convertValue(Class<?> type, String value) {
        if (type == int.class || type == Integer.class) {
            return Integer.parseInt(value);
        } 
        if (type == long.class || type == Long.class) {
            return Long.parseLong(value);
        } 
        if (type == boolean.class || type == Boolean.class) {
            return Boolean.parseBoolean(value);
        } 
        if (type == char.class || type == Character.class) {
            return value.charAt(0); 
        } 
        if (type == short.class || type == Short.class) {
            return Short.parseShort(value);
        } 
        if (type == byte.class || type == Byte.class) {
            return Byte.parseByte(value);
        } 
        if (type == java.util.Date.class) {
        	try {
        		return format.parse(value);
        	}catch(Exception e) {
        		//e.printStackTrace();
        		return null;
        	}
            
        }
        if (type == String.class) {
            return value;
        }
        return null;
    }
     
}
  
